/*
 * @(#)Config.java 0.0.1-SNAPSHOT
 * Copyright Group B
 */

package grupb.upf.util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * <p>This static class Config, it contains the methods to manage
 * the properties of the application, these properties are common.</p>
 *
 * @version    0.0.1-SNAPSHOT
 * @author     Group B
 */
public final class Config {
    /**
     * General properties.
     */
    private static Properties config;

    /**
     * The logger. A new logger <code>grupb.upf.configuration.Config</code> is
     * created to add the logs of this Config.
     */
    private static final Log LOG = LogFactory.getLog(Config.class);

    /**
     * Constructor of the class.
     * @throws IOException if the file is not found.
     */
    public Config() throws IOException {
        config = new Properties();
        final File folder = new File("conf");
        final File file = new File("conf/config.properties");
        folder.mkdir();
        if (!file.exists()) {
            initConfig(file);
        }
        final FileInputStream fis =
                new FileInputStream("conf/config.properties");
        config.load(fis);
        fis.close();
    }
    /**
     * Method to add a property.
     * @param name the name of the property
     * @param value the value of the property
     */
    public static void setProperty(final String name, final String value) {
        config.setProperty(name, value);
    }

    /**
     * Method to get a property.
     * @param name the name of the property
     * @return the value of the property
     */
    public static String getProperty(final String name) {
        return config.getProperty(name);
    }

    /**
     * Method to save the configuration.
     * @throws IOException if the file couldn't be saved
     */
    public static void saveProperties() throws IOException {
        final FileOutputStream fos =
                new FileOutputStream("conf/config.properties", false);
        config.store(fos, "Configuration");
        fos.close();
    }

    /**
     * Method to create and initialize the default values of the config file.
     * @param file the file to create
     * @throws IOException the exception.
     */
    private static void initConfig(final File file) throws IOException {
        final FileWriter fWriter = new FileWriter(file);
        final BufferedWriter bWriter = new BufferedWriter(fWriter);
        bWriter.write("");
        bWriter.close();
        fWriter.close();
        if (LOG.isInfoEnabled()) {
            final StringBuffer sb = new StringBuffer("Config file not found. ")
                    .append("It has been created: ")
                    .append(file.getAbsolutePath());
            LOG.info(sb);
        }
        //The DB is embedded, it is not necessary to set up it.
        config.setProperty("XmlDbDataSource/driverClassName",
                "org.exist.xmldb.DatabaseImpl");
        config.setProperty("XmlDbDataSource/url",
                "xmldb:exist:///db");
        config.setProperty("XmlDbDataSource/user",
                "admin");
        config.setProperty("XmlDbDataSource/password",
                "");
        config.setProperty("photo.folder",
                "myFiles/myPhotos/");
        File f = new File("myFiles/myPhotos");
        f.mkdir();
        config.setProperty("video.folder",
                "myFiles/myVideos/");
        f = new File("myFiles/myVideos");
        f.mkdir();
        config.setProperty("photo.folder.incoming",
                "myFiles/myPhotos/incoming/");
        f = new File("myFiles/myPhotos/incoming");
        f.mkdir();
        config.setProperty("video.folder.incoming",
                "myFiles/myVideos/incoming/");
        f = new File("myFiles/myVideos/incoming");
        f.mkdir();
        saveProperties();
    }
}
